package com.itheima.d10_AnLi.ZengAnliTest;

import java.util.List;
import java.util.Random;

/**
 * @ClassName ZengThreadTest
 * @Author 16799
 * @Date 2025/9/24 20:26
 * @Version 1.0
 * @Description TODO
 **/
public class ZengThreadTest extends Thread {
    private List<String> gift;

    private int count;

    public ZengThreadTest(List<String> gift, String name) {
        super(name);
        this.gift = gift;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();

        Random r = new Random();
        while (true){
            synchronized (gift){
                if (gift.size()<10){
                    break;
                }
            }

            String rs = gift.remove(r.nextInt(gift.size()));
            System.out.println(name + "发出了礼物： " + rs);

            count++;
            try {
                Thread.sleep(100);
            }catch (InterruptedException e){
                throw new RuntimeException(e);
            }
        }
    }

    public int getCount(){
        return count;
    }
    public void setCount(int count){
        this.count = count;
    }
}
